package podcore;

# Internetwork Management Program-Tabbed Telenet/SSH client for internetwork management.
# Copyright (C) 2010 Matt O'Brien
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

use strict;
use warnings;
use Net::Telnet::Cisco;
use Tk;

our %ports = (
	2007 => "frs",
	2009 => "cat1",
	2010 => "cat2",
	2011 => "cat3",
	2012 => "cat4",
	2001 => "r1",
	2002 => "r2",
	2003 => "r3",
	2004 => "r4",
	2005 => "r5",
	2006 => "r6"
	);

sub SaveConfig {
	my ($podnum, $port, $device) = @_;
	printf ("Connecting to %-10s",  "$device...  ");
	my $ip = sprintf("128.213.10.%d", $podnum + 100);
	
	my $session = Net::Telnet::Cisco->new (
		Host => $ip,
		Port => $port,
		Timeout => 100
	);

	$session->print ("\r");
	$session->print ("\r");

	$session->login (Password => "ccie-1824\r\n");
	$session->send_wakeup; #send a carriage return after password prompt
	print "Connected... ";
	
	$session->enable ("ccie-1824"); #If password prompt for enable

	#Allows all data to be displayed at once.
	#I.e, no more '--More--' prompts.
	#The command is different based on 
	#whether it is router or switch.

	$session->cmd ('terminal length 0');

	print "Building Configuration...   ";

	my @output = $session->cmd ("show run");

	#clean up.
	$session->close;
	
	return @output;
}
sub GetNonpropegating {
	my ($podnum, $port, $device) = @_;
	my $ip = sprintf("128.213.10.%d", $podnum + 100);
	
	my $session = Net::Telnet::Cisco->new (
		Host => $ip,
		Port => $port,
		Timeout => 100
	);

	$session->print ("\r");
	$session->print ("\r");

	$session->login (Password => "ccie-1824\r\n");
	$session->send_wakeup; #send a carriage return after password prompt
	
	$session->enable ("ccie-1824"); #If password prompt for enable
	my $is_router = 1;
	my @output = $session->cmd ("sh ip int br");

	for my $line (@output) {
		if ($line =~ /Vlan/) {
			$is_router = 0;
		}
	}
	if ($is_router) {
		my @noshut;
		for my $line (@output) {
			if ($line =~ /([a-zA-Z0-9\/]+) +(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
				push @noshut, $1;
			}
		}
		$session->close;
		return @noshut;
	} 
}

sub Init {
	my ($podnum, $port, $device) = @_;
	my $ip = sprintf("128.213.10.%d", $podnum + 100);
	
	my $session = Net::Telnet::Cisco->new (
		Host => $ip,
		Port => $port,
		Timeout => 100
	);

	$session->print ("\r");
	$session->print ("\r");

	$session->login (Password => "ccie-1824\r\n");
	$session->send_wakeup; #send a carriage return after password prompt
	$session->enable;

	$session->cmd ('config t');
	$session->cmd ("hostname P$podnum\u$device");
	$session->cmd ("no ip domain-lookup");
	$session->cmd ("service password-encryption");
	$session->cmd ("enable secret ccie-1824");
	$session->cmd ("line con 0");
	$session->cmd ("exec-timeout 0 0");
	$session->cmd ("password ccie-1824");
	$session->cmd ("logging synchronous");
	$session->cmd ("end");
}

sub Load {
	my ($podnum, $port, $device, @data) = @_;
	my $ip = sprintf("128.213.10.%d", $podnum + 100);
	printf ("Connecting to %-10s",  "$device...  ");
	my $session = Net::Telnet::Cisco->new (
		Host => $ip,
		Port => $port,
		Timeout => 100
	);
	$session->errmode (sub {return;} );
	print "Connected...   ";

	my $prompt = '/(?m:^\r?[\w.-]+\s?(?:\(.*[^\)]*\))?\s?[\$#>]\s?(?:\(enable\))?\s*$)/';

	$session->print ("\r");
	$session->print ("\r");

	$session->login (Password => "ccie-1824\r\n");
	$session->send_wakeup; #send a carriage return after password prompt
	$session->prompt ($prompt);
	$session->ignore_warnings;
	$session->enable if !$session->is_enabled;
	$session->cmd ('config t');
	print "Loading configuration...   ";
	foreach my $command (@data) {
		$session->cmd ($command);
		$session->always_waitfor_prompt;
	}
	print "Done\n";
}

1;
